/*
 * @Author: biankai
 * @Date: 2021-10-19 16:16:18
 * @LastEditTime: 2021-11-05 14:11:18
 * @LastEditors: biankai
 * @FilePath: /gitee/devops_service/config/KubernetesConnect.js
 */

var kubernetes = {
    // 初始化CoreApi
    initCoreV1Api: () => {
        const k8s = require('@kubernetes/client-node');
        const kc = new k8s.KubeConfig();
        kc.loadFromFile('./140config')
        const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
        return k8sApi;
    },
    // 初始化AppApi
    initAppV1Api: () => {
        const k8s = require('@kubernetes/client-node');
        const kc = new k8s.KubeConfig();
        kc.loadFromFile('./140config')
        const k8sApi = kc.makeApiClient(k8s.AppsV1Api);
        return k8sApi;
    },
    loadYaml: () => {
        const k8s = require('@kubernetes/client-node');
        return k8s.loadYaml(yamlString);
    },
    // 命名空间
    namespace: {
        list: () => {
            const k8s = kubernetes.initCoreV1Api();
            k8s.listNamespace().then((res) => {
                console.log(res.body);
            });
        },
        create: () => {
            var namespace = {
                metadata: {
                    name: 'test'
                }
            };
            const k8s = kubernetes.initCoreV1Api();
            k8s.createNamespace(namespace).then((res) => {
                console.log(res.body);
            });
        },
        read: () => {
            const k8s = kubernetes.initCoreV1Api();
            k8s.readNamespace('test').then((res) => {
                console.log(res.body);
            });
        },
        delete: () => {
            const k8s = kubernetes.initCoreV1Api();
            k8s.deleteNamespace('test').then((res) => {
                console.log(res.body);
            });
        }
    },
    // 部署
    deployment: {
        list: () => {
            const k8sApi = kubernetes.initAppV1Api()
            k8sApi.listNamespacedDeployment('test').then((res) => {
                console.log(res.body);
            });
        },
        create: () => {
            const fs = require('fs')
            const YAML = require('yaml')
            const file = fs.readFileSync('./k8_deployment.yaml', 'utf8')

            var deployment = YAML.parse(file)
            console.info(JSON.stringify(deployment))
            // const k8sApi = kubernetes.initAppV1Api()
            // k8sApi.createNamespacedDeployment('test', deployment).then((res) => {
            //     console.log(res.body);
            // }).catch(() => { });
        },
        read: () => {
            const k8sApi = kubernetes.initAppV1Api()
            k8sApi.readNamespacedDeployment('devops', 'test').then((res) => {
                console.log(res.body);
            });
        },
        replace: () => {
            const fs = require('fs')
            const YAML = require('yaml')
            const file = fs.readFileSync('./k8_deployment.yaml', 'utf8')
            var deployment = YAML.parse(file)
            const k8sApi = kubernetes.initAppV1Api()
            k8sApi.replaceNamespacedDeployment('devops', 'test', deployment).then((res) => {
                console.log(res.body);
            });
        },
        delete: () => {
            const k8sApi = kubernetes.initAppV1Api()
            k8sApi.deleteNamespacedDeployment('devops', 'test').then((res) => {
                console.log(res.body);
            });
        }
    },
    // 服务
    service: {
        list: () => {
            const k8sApi = kubernetes.initCoreV1Api()
            k8sApi.listNamespacedService('test').then((res) => {
                console.log(res.body);
            });
        },
        create: () => {
            const fs = require('fs')
            const YAML = require('yaml')
            const file = fs.readFileSync('./k8_service.yaml', 'utf8')
            var service = YAML.parse(file)

            console.info(JSON.stringify(service))
            // const k8sApi = kubernetes.initCoreV1Api()
            // k8sApi.createNamespacedService('test', service).then((res) => {
            //     console.log(res.body);
            // }).catch((error) => {
            //     console.error(error.body)
            // });
        },
        read: () => {
            const k8sApi = kubernetes.initCoreV1Api()
            k8sApi.readNamespacedService('devops', 'test').then((res) => {
                console.log(res.body);
                console.log(res.body.metadata.managedFields);
                console.log(res.body.spec.clusterIP + ':' + res.body.spec.ports[0].nodePort);
            });
        },
        replace: () => {
            const fs = require('fs')
            const YAML = require('yaml')
            const file = fs.readFileSync('./k8_service.yaml', 'utf8')
            var service = YAML.parse(file)
            const k8sApi = kubernetes.initCoreV1Api()
            k8sApi.replaceNamespacedService('devops', 'test', service).then((res) => {
                console.log(res.body);
            });
        },
        delete: () => {
            const k8sApi = kubernetes.initCoreV1Api()
            k8sApi.deleteNamespacedService('devops', 'test').then((res) => {
                console.log(res.body);
            });
        }
    }
}
kubernetes.service.create();


